package com.newshub.business.service.impl;

import com.newshub.business.dto.PageResult;
import com.newshub.business.dto.RegionQueryDTO;
import com.newshub.business.entity.Region;
import com.newshub.business.mapper.RegionMapper;
import com.newshub.business.service.RegionService;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 区域服务实现类
 */
@Service
public class RegionServiceImpl implements RegionService {
    
    @Autowired
    private RegionMapper regionMapper;
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "获取所有区域")
    public List<Region> getAllRegions() {
        return regionMapper.selectAllRegions();
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "根据ID获取区域")
    public Region getRegionById(Integer id) {
        return regionMapper.selectById(id);
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "根据名称获取区域")
    public Region getRegionByName(String name) {
        return regionMapper.selectByName(name);
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "根据代码获取区域")
    public Region getRegionByCode(String code) {
        return regionMapper.selectByCode(code);
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "添加区域")
    public boolean addRegion(Region region) {
        // 设置默认值
        if (region.getStatus() == null) {
            region.setStatus(1); // 默认启用
        }
        if (region.getSortOrder() == null) {
            region.setSortOrder(0); // 默认排序
        }
        if (region.getCreatedAt() == null) {
            region.setCreatedAt(LocalDateTime.now());
        }
        if (region.getUpdatedAt() == null) {
            region.setUpdatedAt(LocalDateTime.now());
        }
        
        return regionMapper.insert(region) > 0;
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "更新区域")
    public boolean updateRegion(Region region) {
        region.setUpdatedAt(LocalDateTime.now());
        return regionMapper.updateById(region) > 0;
    }
    
    @Override
    @BusinessLog(level = LogLevel.WARN, description = "删除区域")
    public boolean deleteRegion(Integer id) {
        return regionMapper.deleteById(id) > 0;
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "分页查询区域")
    public PageResult<Region> getRegionPage(RegionQueryDTO queryDTO) {
        // 计算偏移量
        int offset = (queryDTO.getPageNum() - 1) * queryDTO.getPageSize();
        
        // 查询数据
        List<Region> list = regionMapper.selectByPage(
            queryDTO.getName(), 
            queryDTO.getCode(), 
            queryDTO.getStatus(), 
            offset, 
            queryDTO.getPageSize()
        );
        
        // 查询总数
        long total = regionMapper.countByPage(
            queryDTO.getName(), 
            queryDTO.getCode(), 
            queryDTO.getStatus()
        );
        
        // 返回分页结果
        return PageResult.of(queryDTO.getPageNum(), queryDTO.getPageSize(), total, list);
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "更新区域状态")
    public boolean updateRegionStatus(Integer id, Integer status) {
        return regionMapper.updateStatusById(id, status) > 0;
    }
    
    @Override
    @BusinessLog(level = LogLevel.WARN, description = "批量删除区域")
    public boolean batchDeleteRegions(List<Integer> ids) {
        return regionMapper.deleteBatchByIds(ids) > 0;
    }
}
